package leecode

func queensAttacktheKing(queens [][]int, king []int) (result [][]int) {
	row := king[0]    //国王所在行
	column := king[1] //国王所在列
	//左上方斜对角线
	for i, j := row, column; i > 0 && j > 0; i, j = i-1, j-1 {
		if isExists(queens, i-1, j-1) {
			temp := []int{i - 1, j - 1}
			result = append(result, temp)
			break
		}
	}
	//正上方直线
	for i := row; i > 0; i-- {
		if isExists(queens, i-1, column) {
			temp := []int{i - 1, column}
			result = append(result, temp)
			break
		}
	}
	//右上方斜对角线
	for i, j := row, column; i > 0 && j < 7; i, j = i-1, j+1 {
		if isExists(queens, i-1, j+1) {
			temp := []int{i - 1, j + 1}
			result = append(result, temp)
			break
		}
	}
	//右方
	for j := column; j < 7; j++ {
		if isExists(queens, row, j+1) {
			temp := []int{row, j + 1}
			result = append(result, temp)
			break
		}
	}
	//右下方斜线
	for i, j := row, column; i < 7; i, j = i+1, j+1 {
		if isExists(queens, i+1, j+1) {
			temp := []int{i + 1, j + 1}
			result = append(result, temp)
			break
		}
	}
	//正下方
	for i := row; i < 7; i++ {
		if isExists(queens, i+1, column) {
			temp := []int{i + 1, column}
			result = append(result, temp)
			break
		}
	}
	//左下
	for i, j := row, column; i < 7 && j > 0; i, j = i+1, j-1 {
		if isExists(queens, i+1, j-1) {
			temp := []int{i + 1, j - 1}
			result = append(result, temp)
			break
		}
	}
	//左
	for j := column; j > 0; j-- {
		if isExists(queens, row, j-1) {
			temp := []int{row, j - 1}
			result = append(result, temp)
			break
		}
	}
	return
}

func isExists(queens [][]int, row, column int) bool {
	for i := 0; i < len(queens); i++ {
		if queens[i][0] == row && queens[i][1] == column {
			return true
		}
	}
	return false
}
